<?php

include_once('HTGPage.class.php');

/**
 * 
foreach($_REQUEST as $key => $value) {
	echo 'key: ' . $key . ': value: ' . $value . '<br />';
}
 */


$page = new HTGPage();

$security = $page->mySecurity;

/* @var $utils Utils */
$utils = $page->myUtils;

if(!($security->isAdmin())) {
	$page->redirectTo("index.php");
}

$utils->recordPOST();

switch(@$_REQUEST['action']) {
	case 'delete':
		$pickId = $_REQUEST['p_id'];
		$utils->deletePick($pickId);
		break;
	case 'mkwin':
		$pickId = $_REQUEST['p_id'];
		$utils->makePickWinner($pickId);
		break;
	case 'mklose':
		$pickId = $_REQUEST['p_id'];
		$utils->makePickLoser($pickId);
		break;
	case 'mkpush':
		$pickId = $_REQUEST['p_id'];
		$utils->makePickPush($pickId);
		break;
		
	default:
		//nothing
		break;
}

if(@$_REQUEST['action'] == 'delete') { //deleting the pick
}

if(@$_REQUEST['picktext']) {
  //add that shit to the db
	$visteam = $_REQUEST['visteam'];
	$hometeam = $_REQUEST['hometeam'];
	$starttime = $_REQUEST['starttime'];
	$publictime = $_REQUEST['publictimeinterval'];
	$subject = "#"; //$_REQUEST['subject'];
	$text = $_REQUEST['picktext'];
	$pick = $_REQUEST['pick'];
	$submit = $_REQUEST['submit'];
    $sport = $_REQUEST['sport'];
    $league = $_REQUEST['league'];
    $gametype = $_REQUEST['gametype'];
	$isHTPick = $_REQUEST['htpick'];

	
	
	
	$publicDate = false;
	$goPublicDate = $_REQUEST['publicdate'];
	$specificPublicDate = $_REQUEST['publicdatedate'];

	if(!(empty($specificPublicDate))) {
		$publicDate = strtotime($specificPublicDate);
	} else {
		if(strstr($goPublicDate, 's')) {
			//value without s is minutes after start date
			$secondsToDelay = 60 * str_replace('s', '', $goPublicDate);
			$startTimeUnix = strtotime($starttime) + $secondsToDelay;
			$publicDate = $startTimeUnix;
		} else {
			//values is minutes from now
			$secondsToDelay = 60 * $goPublicDate;	
			$publicDate = time() + $secondsToDelay;		
		}
	}
	
	
    $halftimepick = $_REQUEST['halftimepick'];
	$halftimepicktext = $_REQUEST['halftimepicktext'];

	$eventId = $_REQUEST['eventId'];
	
    $eventType = $league;  //1 is football
	
    $pickType = 1;
    if($isHTPick == 'true') {
    	$pickType = 2;
    }
    
    //jQuery datepicker determines format, thus we must parse to pass to method
    //08/18/2011 02:00 pm
    $starttime = strftime("%G-%m-%d %H:%M:%S", strtotime($starttime));
    error_log(__FILE__ . " " . __LINE__ . ' start: ' . $starttime);
    //addPick requires starttime be a string YYYY-mm-dd HH:MM:SS

    $publicDate = strftime("%G-%m-%d %H:%M:%S", $publicDate);
    error_log($publicDate);
    
    	$result = $page->myUtils->addPick($sport, $league, $gametype, $visteam, $hometeam, $starttime, $pick, $subject, $text, $eventType, $pickType, $publicDate);
    
    //send message
    /* @var $myNotificaion HTGNotification */
//     $myNotification = $page->myNotification;
    
//     $numbers = $page->myUtils->getPaidMemberNumbers();
//     $myNotification->notify($numbers, $pick);
	$emails = $utils->getPaidMemberEmails();
	print_r($emails);

	//$emails = $page->myUtils->getPaidMemberEmails();
	$page->content .= '<br /> Paid Customers Notified:' . count($emails) . '<br/>';
	error_log("emails: " . count($emails));
	$subject = $visteam . '@' . $hometeam . ' HalfTime Genius Notification';
	$pickPage = '';
	switch ($league) {
		case 2:
			$pickPage='ncaaf.php?r=pne'; //pne pick notification email
			break;
		default:
			$pickPage='nfl.php?r=pne'; //pne pick notification email
			break;
	}

	$body = "We've posted the pick on the " . $visteam . '@' . $hometeam . ' game.  Go Check it out at http://halftimegenius.com/' . $pickPage;
	$headers = 'From: "HalfTime Genius" <halftimegenius@gmail.com>' . "\r\n" .
				    'Reply-To: halftimegenius@gmail.com' . "\r\n" .
				    'X-Mailer: PHP/' . phpversion();

	foreach($emails as $emailRecord) {
		$page->content .= "mailing: " . $emailRecord['u_email'] . "<br />\r\n";
		error_log("mailing: " . $emailRecord['u_email']);
		mail($emailRecord['u_email'], $subject, $body, $headers);
		mail("chris.mondor@gmail.com", $subject, $body, $headers);
	}
    	
    $page->content .= '<div id="myMainContent"><p><a href="' . $_SERVER['PHP_SELF'] . '">Pick added ' . $result . 'Successfully -- Add Another</a></p></div>';
}

	$page->content .= $page->myUtils->get_include_contents('addPicksForm.php');
	
// 	$page->content_wrongvarname = '
// <style type="text/css">
// #myMainContent {
//   width: 100%;
//   display: block;
//   margin-left: auto;
//   margin-right: auto;
// }

// #myTable {
//   margin-left: auto;
//   margin-right: auto;
//   text-align: left;
// }

// #myTable .label {
//   margin-left: auto;
//   margin-right: auto;
//   text-align: right;
// }


// .pickForm input[type="submit"] {
//   margin-left: auto;
//   margin-right: auto;
// }


// .ui-timepicker-div .ui-widget-header{ margin-bottom: 8px; }
// .ui-timepicker-div dl{ text-align: left; }
// .ui-timepicker-div dl dt{ height: 25px; }
// .ui-timepicker-div dl dd{ margin: -25px 0 10px 65px; }
// .ui-timepicker-div td { font-size: 90%; }

// </style>

// <div id="myMainContent">
// <table id="myTable" >
// <tr>
// <td colspan=2><h2>Add a Pick</td>
// </tr>
// <tr>
// <td>
// <form method="post" id="pickForm" action="' . $_SERVER['PHP_SELF'] . '">
// <fieldset name="">
// Previous Pick Games:<select name="eventId"><option value="select">Select Game</option>';

// $events = $page->myUtils->getRecentEvents();
// $eventIds = array();	
// foreach($events as $i => $event) {
// 	if(!(in_array($event['e_id'], $eventIds))) {
// 	$page->content .= '<option value="' . $event['e_id'] . '">' . $event['e_starttime'] . ' ' . $event['e_vis_team'] . '@' . $event['e_home_team'] . '</option>';
// 	array_push($eventIds, $event['e_id']);
// 	}
// }

// $page->content_wrongvarname .= '</select>
// Sport:<select name="sport">
// <option value="1">Football</option>
// </select>
// League:<select name="league">
// <option value="1">NFL</option>
// <option value="2">NCAA</option>
// </select>
// Game Type:<select name="gametype">
// <option value="1">Regular Season</option>
// <option value="2">Pre Season</option>
// <option value="3">Post Season</option>
// </select>
// Visiting Team:<input type="text" name="visteam" value="" />
// Home Team:<input type="text" name="hometeam" value=""/>
// Start Time:<div><input type="text" id="starttime" name="starttime" value=""/></div>
// </fieldset>
// </td>
// </tr>
// <tr>
// <td>
// <fieldset name="isHalftimeFieldSet">
// Is Half Time pick?:&nbsp;Yes: <input type="radio" name="htpick" value="true" Yes />No: <input type="radio" name="htpick" value="false" checked/> &nbsp; &nbsp; 
// </fieldset>
// </td>
// </tr>

// <tr>
// <td class="label">
// <fieldset>
// Pick:<input type="text" name="pick" value=""/>
// Pick Subject:<input type="text" name="subject" value=""/>
// Pick Text:<textarea name="picktext" rows="10" cols="52"></textarea>
// </fieldset>
// </td>
// <td colspan=2 align=center><input type="submit" name="submit" value="submit" /></td>
// </tr>
// </table>
// </form>
// </div>
// <div>
// ';

$page->content .= '<br /><div id="pickTableDiv">' . $page->myUtils->returnTableOfPicks() . '</div>';

$page->preHeadCloseContent = '
<!--  jQuery datetime picker -->
	<link type="text/css" href="addPicks.css" rel="stylesheet" />
	<link type="text/css" href="css/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
	<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
	<script type="text/javascript" src="js/timepicker.js"></script>
	

 <script type="text/javascript">
 Date.format = \'yyyy-mm-dd\';
$(function() {
    $(\'#starttime\').datepicker({
    	duration: \'\',
        showTime: true,
        constrainInput: false
     });

         $(\'#publicdatedate\').datepicker({
    	duration: \'\',
        showTime: true,
        constrainInput: false
     });

     
});
</script>
<script type="text/javascript">
function validateForm()
{
var starttimeFieldValue = document.forms["pickForm"]["starttime"].value;
if(!(starttimeFieldValue)) {
  alert("Please Fill Out the Start Time Properly");
  return false;
}
}
</script>

';


$page->display();
?>